﻿#include "enlargeui.h"
#include "ui_enlargeui.h"

enlargeUI::enlargeUI(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::enlargeUI)
{
    ui->setupUi(this);

    screenImageMutex = new QMutex();

    imageZoom = 0;
    isFlush = false;
    isLock = false;
    curX = 0;
    curY = 0;
    setWindowFlag(Qt::WindowStaysOnTopHint);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setWindowFlags(windowFlags()&~Qt::WindowMinimizeButtonHint);
}

enlargeUI::~enlargeUI()
{
    delete screenImageMutex;
    delete ui;
}

void enlargeUI::moveEvent(QMoveEvent *event){
    //窗口移动后自己的业务代码
    retInfo(QString("moveEvent %0 %1 - %2 %3")
                .arg(event->pos().x()).arg(event->pos().y())
                .arg(this->pos().x()).arg(this->pos().y()));
    emit sendMove(this->pos().x(),this->pos().y());
}

void enlargeUI::resizeEvent(QResizeEvent *event){
    int w = event->size().width();
    int h = event->size().height();
    retInfo(QString("resizeEvent %0 %1").arg(w).arg(h));
    emit sendSize(w,h);
}

void enlargeUI::setZoom(float zoom){
    imageZoom = zoom;
    ui->doubleSpinBoxZoom->setValue(imageZoom);
}

void enlargeUI::setWin(int x,int y,int w,int h,int winW,int winH){
    winWidth = winW;
    winHeight = winH;
    retInfo(QString("enlarge %0 %1 %2 %3 win %4 %5").arg(x).arg(y).arg(w).arg(h).arg(winWidth).arg(winHeight));

    int posX = x, posY = y;
    if(x<0){
        posX = winWidth + x - w;
    }
    if(y<0){
        posY = winHeight + y - h;
    }

    retInfo(QString("enlarge %0 %1").arg(posX).arg(posY));
    // resize(w,h);
    setFixedSize(w,h);
    move(posX,posY);
}

void enlargeUI::recvImageDoubleClicked(void){
    isLock = !isLock;
    if(isLock){
        ui->labelState->setText("锁");
    }else{
        ui->labelState->setText(" ");
    }
}

void enlargeUI::recvImageCursorPos(int x,int y){
    if(x<=0 || y<=0)return;
    if(isLock)return;
    if(!screenImageMutex->tryLock()){
        return;
    }
    //获取
    curX = x;
    curY = y;

    isFlush = true;
    ui->labelCoordinateInfo->setText(QString("X:%0, Y:%1").arg(curX,4).arg(curY,4));

    screenImageMutex->unlock();
}

void enlargeUI::closeEvent(QCloseEvent *event)
{
    // event->accept();
    event->ignore();
    emit sendExit();
}

void enlargeUI::flushImage(void){
    int width,height;
    width = ui->labelImage->width();
    height = ui->labelImage->height();

    if(!isFlush)
        return;

    if(!screenImageMutex->tryLock()){
        return;
    }

    cv::Mat src = curEnlargeImage;


    float scaleW,scaleH;
    scaleW = (float)src.cols/(float)winWidth;
    scaleH = (float)src.rows/(float)winHeight;
    int x,y;
    x = (float)curX * scaleW;
    y = (float)curY * scaleH;

    // retInfo(QString("curEnlargeImage %0 %1 image %2 %3 win %4 %5 %6 %7")
    //             .arg(src.cols).arg(src.rows).arg(width).arg(height)
    //             .arg(winWidth).arg(winHeight).arg(getNumber(scaleW)).arg(getNumber(scaleH)));


    if(src.channels() == 3){
        int r,g,b;
        b = src.at<cv::Vec3b>(y,x)[0];
        g = src.at<cv::Vec3b>(y,x)[1];
        r = src.at<cv::Vec3b>(y,x)[2];
        ui->labelPixelInfo->setText(QString("R:%0, G:%1, B:%2").arg(r,3).arg(g,3).arg(b,3));
    }else{
        ui->labelPixelInfo->setText(QString("gray:%0").arg(src.at<uchar>(y,x)));
    }

    cv::Rect box;

    box.x = x - width/2;
    box.y = y - height/2;
    box.width = width;
    box.height = height;

    if(box.x < 0){
        box.x = 0;
    }
    if(box.y < 0){
        box.y = 0;
    }
    if((box.x + width) >= src.cols){
        box.width = src.cols - box.x -1;
    }
    if((box.y + height) >= src.rows){
        box.height = src.rows - box.y -1;
    }
    // retInfo(QString("curEnlargeImage %0 %1 %2 %3 zoom %4 image %5 %6")
    //             .arg(box.x).arg(box.y).arg(box.width).arg(box.height).arg(getNumber(imageZoom)).arg(src.cols).arg(src.rows));
    cv::Mat image = src(box);
    if(imageZoom>1.0){
        //放大
        cv::Size dsize = cv::Size(image.cols*imageZoom,image.rows*imageZoom);
        cv::Mat src = cv::Mat(dsize, image.type());
        cv::resize(image, src, dsize);
        //        retInfo(QString("zoom %0 newImage %1 %2").arg(getNumber(imageZoom)).arg(src.cols).arg(src.rows));
        box.x = 0;
        box.y = 0;
        box.width = width;
        box.height = height;
        if(src.cols>width){
            box.x = (src.cols - width)/2;
        }else{
            box.width = src.cols - 1;
        }
        if(src.rows>height){
            box.y = (src.rows - height)/2;
        }else{
            box.height = src.rows - 1;
        }
        image = src(box);
    }
    //黑背景
    cv::Mat black = cv::Mat::zeros(cv::Size(width,height),src.type());
    cv::Rect rect(black.cols-image.cols,black.rows-image.rows,image.cols,image.rows);
    //粘贴图
    image.copyTo(black(rect));
    // 画线
    for(int i=1; i<black.cols; i+=5){
        cv::line(black,cv::Point(i,black.rows/2),cv::Point(i+2,black.rows/2),cv::Scalar(127));
    }
    for(int i=1; i<black.rows; i+=5){
        cv::line(black,cv::Point(black.cols/2,i),cv::Point(black.cols/2,i+2),cv::Scalar(127));
    }
    ui->labelImage->setPixmap(cvMatToQPixmap(image));

    isFlush = false;

    screenImageMutex->unlock();
}

void enlargeUI::paintEvent(QPaintEvent *event){
    //    QPainter painter(this);
    Q_UNUSED(event);
    flushImage();
}


void enlargeUI::on_doubleSpinBoxZoom_valueChanged(double arg1)
{
    emit sendZoom(arg1);
}

